Form.Element.Serializers.selectOne = function(element) {
    var value = '', opt, index = element.selectedIndex;
    if (index >= 0) {
        opt = element.options[index];
        value = opt.value;
    }
    return [element.name, value];
}

Form.Element.Serializers.selectMany = function(element) {
    var value = [];
    for (var i = 0; i < element.length; i++) {
        var opt = element.options[i];
        if (opt.selected)
            value.push(opt.value || opt.text);
    }
    return [element.name, value];
}

Object.extend(Event, {
    _domReady : function() {
        if (arguments.callee.done) return;
        arguments.callee.done = true;

        if (this._timer)  clearInterval(this._timer);

        this._readyCallbacks.each(function(f) { f() });
        this._readyCallbacks = null;
        this._loaded = true;
    },

    onDOMReady : function(f) {
        if (this._loaded) {
            f();
            return ;
        }
        if (!this._readyCallbacks) {
            var domReady = this._domReady.bind(this);

            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", domReady, false);

            /*@cc_on @*/
            /*@if (@_win32)
              document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
              document.getElementById("__ie_onload").onreadystatechange = function() {
              if (this.readyState == "complete") domReady();
              };
              /*@end @*/

            if (/WebKit/i.test(navigator.userAgent)) {
                this._timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState)) domReady();
                }, 10);
            }

            Event.observe(window, 'load', domReady);
            Event._readyCallbacks =  [];
        }
        Event._readyCallbacks.push(f);
    }
});

Element.addMethods(
{
    update: function(element, html) {
        html = typeof html == 'undefined' ? '' : html.toString();
        $(element).innerHTML = html.stripScripts();
        setTimeout(function() {
            html.evalScripts();
            try {
                baseAjax.ajaxify();
            } catch (e) {
            }
        }, 10);
        return element;
    },

    
    replace: function(element, html) 
    {
        element = $(element);
        html = typeof html == 'undefined' ? '' : html.toString();
        if (element.outerHTML) {
            element.outerHTML = html.stripScripts();
        } else {
            var range = element.ownerDocument.createRange();
            range.selectNodeContents(element);
            element.parentNode.replaceChild(
                                            range.createContextualFragment(html.stripScripts()), element);
        }
        setTimeout(function() {
            html.evalScripts();
            try {
                baseAjax.ajaxify();
            } catch (e) {
            }
        }, 10);

        return element;
    },
    
    /* This is much faster than using (el.innerHTML = value) when there are many
    existing descendants, because in some browsers, innerHTML spends much longer
    removing existing elements than it does creating new ones. */
    replaceHtml: function (element, html) {
        var oldElement = (typeof element === "string" ? document.getElementById(element) : element);
        /*@cc_on // Pure innerHTML is slightly faster in IE
        oldElement.innerHTML = html;
        return oldElement;
        @*/
        var newElement = oldElement.cloneNode(false);
        newElement.innerHTML = html;
        oldElement.parentNode.replaceChild(newElement, oldElement);
        /* Since we just removed the old element from the DOM, return a reference
        to the new element, which can be used to restore variable references. */
        
        setTimeout(function() {
                
            html = typeof html == 'undefined' ? '' : html.toString();
            html.evalScripts();
            
            try {
                baseAjax.ajaxify(newElement);
            } catch (e) {
            }
        }, 10);
        
        return newElement;
    }
});
